<template>
  <div class="app-container">
    <div class="head-container">
      <el-input
        v-model="listQuery.Filter"
        clearable
        size="small"
        placeholder="搜索..."
        style="width: 200px"
        class="filter-item"
        @keyup.enter.native="handleFilter"
      />
      <el-button
        class="filter-item"
        size="mini"
        type="success"
        icon="el-icon-search"
        @click="handleFilter"
        >搜索</el-button
      >
      <div style="padding: 6px 0">
        <el-button
          class="filter-item"
          size="mini"
          type="primary"
          icon="el-icon-plus"
          @click="handleCreate"
          v-permission="['AbpTenantManagement.Tenants.Create']"
          >新增</el-button
        >
      </div>
    </div>

    <el-dialog
      :visible.sync="dialogFormVisible"
      :close-on-click-modal="false"
      :title="formTitle"
      @close="cancel()"
      width="700px"
    >
      <el-form
        ref="form"
        :inline="true"
        :model="form"
        :rules="rules"
        size="small"
        label-width="90px"
      >
          <el-row :gutter="15">
    <el-col :span="12">
      <el-form-item label="专区名称" prop="prefectureName">
        <el-input v-model="form.prefectureName" placeholder="请输入专区名称" clearable :style="{width: '100%'}">
        </el-input>
      </el-form-item>
    </el-col>
  </el-row>
  <el-row :gutter="15">
    <el-col :span="12">
      <el-form-item label="专区描述" prop="prefecturedescribe">
        <el-radio-group v-model="form.prefecturedescribe" size="medium">
          <el-radio v-model="radio" label="新窗口打开"
                  >新窗口打开</el-radio
                >
                <el-radio v-model="radio" label="同窗口打开"
                  >同窗口打开</el-radio
                >
        </el-radio-group>
      </el-form-item>
    </el-col>
  </el-row>
  <el-row :gutter="15">
    <el-col :span="12">
      <el-form-item label="请输入排序" prop="prefectureOrder">
        <el-input-number v-model="form.prefectureOrder" placeholder="请输入排序"></el-input-number>
      </el-form-item>
    </el-col>
  </el-row>
  <el-row :gutter="15">
    <el-col :span="12">
      <el-form-item label="状态" prop="prefecturestate">
        <el-radio-group v-model="form.prefecturestate" size="medium">
         <el-radio v-model="radio" label="1"
                  >开</el-radio
                >
                <el-radio v-model="radio" label="0"
                  >关</el-radio
                >
        </el-radio-group>
      </el-form-item>
    </el-col>
  </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" type="text" @click="cancel">取消</el-button>
        <el-button
          size="small"
          v-loading="formLoading"
          type="primary"
          @click="save"
          >确认</el-button
        >
      </div>
    </el-dialog>

    <el-table
      ref="multipleTable"
      v-loading="listLoading"
      :data="list"
      size="small"
      style="width: 90%"
      @sort-change="sortChange"
    >
      <el-table-column
        label="序号"
        prop="id"
        sortable="custom"
        align="center"
        width="150px"
      >
        <template slot-scope="{ row }">
          <span class="link-type" @click="handleUpdate(row)">{{ row.id }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="专区名称"
        prop="prefectureName"
        sortable="custom"
        align="center"
        width="150px"
      >
        <template slot-scope="{ row }">
          <span class="link-type" @click="handleUpdate(row)">{{
            row.prefectureName
          }}</span>
        </template>
      </el-table-column>
       <el-table-column
        label="专区描述"
        prop="prefecturedescribe"
        sortable="custom"
        align="center"
        width="150px"
      >
        <template slot-scope="{ row }">
          <span class="link-type" @click="handleUpdate(row)">{{
            row.prefecturedescribe
          }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="排序"
        prop="prefectureOrder"
        sortable="custom"
        align="center"
        width="150px"
      >
        <template slot-scope="{ row }">
          <span class="link-type" @click="handleUpdate(row)">{{
            row.prefectureOrder
          }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="状态"
        prop="prefecturestate"
        sortable="custom"
        align="center"
        width="150px"
      >
        <template slot-scope="scope">
          <!-- <span class="link-type" @click="handleUpdate(row)">{{row.headState}}</span> -->
          <el-switch
            v-model="scope.row.prefecturestate"
            active-color="#13ce66"
            inactive-color="#ff4949"
            :active-value="1"
            active-text="正常"
            :inactive-value="0"
            inactive-text="禁用"
            @change="xgzt(scope.row)"
          >
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template slot-scope="{ row }">
          <el-button
            type="primary"
            size="mini"
            @click="handleUpdate(row)"
            v-permission="['AbpTenantManagement.Tenants.Update']"
            icon="el-icon-edit"
          />
          <el-button
            type="danger"
            size="mini"
            @click="handleDelete(row)"
            v-permission="['AbpTenantManagement.Tenants.Delete']"
            icon="el-icon-delete"
          />
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="totalCount > 0"
      :total="totalCount"
      :page.sync="page"
      :limit.sync="listQuery.MaxResultCount"
      @pagination="getList"
    />
  </div>
</template>

<script>
import Pagination from "@/components/Pagination";
import permission from "@/directive/permission/index.js";

const defaultForm = {
  id: null,
  adminEmailAddress: "",
  adminPassword: "",
  name: "",
};
export default {
  name: "Tenant",
  components: { Pagination },
  directives: { permission },
  data() {
    return {
      rules: {
        name: [{ required: true, message: "请输入租户名", trigger: "blur" }],
      },
      form: Object.assign({}, defaultForm),
      list: null,
      totalCount: 0,
      listLoading: true,
      formLoading: false,
      listQuery: {
        Filter: "",
        Sorting: "",
        SkipCount: 0,
        MaxResultCount: 10,
      },
      page: 1,
      dialogFormVisible: false,
      multipleSelection: [],
      formTitle: "",
      isEdit: false,
    };
  },
  created() {
    this.getList();
    this.fetchData(id);
  },
  methods: {
    xgzt(row)
    {
       alert(row.id);
       this.$axios

              .puts("https://localhost:44358/api/app/prefectures/"+row.id+"/state")
              .then((response) => {
                this.formLoading = false;
                this.$notify({
                  title: "成功",
                  message: "更新状态成功",
                  type: "success",
                  duration: 2000,
                });
                this.dialogFormVisible = false;
                this.getList();
              })
              .catch(() => {
                this.formLoading = false;
              });
    } ,
    getList() {
      this.listLoading = true;
      this.listQuery.SkipCount =
        (this.page - 1) * this.listQuery.MaxResultCount;
      this.$axios
        .gets(
          "https://localhost:44358/api/app/prefectures/buttom-lists",
          this.listQuery
        )
        .then((response) => {
          console.log(response);
          this.list = response;
          this.totalCount = response.totalCount;
          this.listLoading = false;
        });
    },
    fetchData(id) {
      this.$axios.posts("https://localhost:44358/api/app/prefectures/" + id+"/fantian").then((response) => {
        this.form = response;
      });
    },
    handleFilter() {
      this.page = 1;
      this.getList();
    },
    save() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          this.formLoading = true;
          this.form.roleNames = this.checkedRole;
          if (this.isEdit) //修改
          {
            this.$axios

              .puts("https://localhost:44358/api/app/prefectures/" + this.form.id+"/prefectures", this.form)
              .then((response) => {
                this.formLoading = false;
                this.$notify({
                  title: "成功",
                  message: "更新成功",
                  type: "success",
                  duration: 2000,
                });
                this.dialogFormVisible = false;
                this.getList();
              })
              .catch(() => {
                this.formLoading = false;
              });
          } //添加
          else
          {
            this.$axios
              .posts("https://localhost:44358/api/app/prefectures/prefectures", this.form)
              .then((response) => {
                this.formLoading = false;
                this.$notify({
                  title: "成功",
                  message: "新增成功",
                  type: "success",
                  duration: 2000,
                });
                this.dialogFormVisible = false;
                this.getList();
              })
              .catch(() => {
                this.formLoading = false;
              });
          }
        }
      });
    },
    handleCreate() {
      this.formTitle = "新增租户";
      this.isEdit = false;
      this.dialogFormVisible = true;
    },
   handleDelete(row) {
      this.$confirm("是否删除" + row.name + "?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.$axios
            .deletes("https://localhost:44358/api/app/prefectures/" + row.id+"/prefectures")
            .then((response) => {
              this.$notify({
                title: "成功",
                message: "删除成功",
                type: "success",
                duration: 2000,
              });
              this.getList();
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    handleUpdate(row) {
      this.formTitle = "修改租户";
      this.isEdit = true;
      if (row) {
        this.fetchData(row.id);
        this.dialogFormVisible = true;
      } else {
        if (this.multipleSelection.length != 1) {
          this.$message({
            message: "编辑必须选择单行",
            type: "warning",
          });
          return;
        } else {
          this.fetchData(this.multipleSelection[0].id);
          this.dialogFormVisible = true;
        }
      }
    },
    sortChange(data) {
      const { prop, order } = data;
      if (!prop || !order) {
        this.handleFilter();
        return;
      }
      this.listQuery.Sorting = prop + " " + order;
      this.handleFilter();
    },
    cancel() {
      this.form = Object.assign({}, defaultForm);
      this.dialogFormVisible = false;
      this.$refs.form.clearValidate();
    },
  },
};
</script>